package com.wxp.demo05.text;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.wxp.demo05.R;

/**
 * 3.3 编辑框焦点操作
 */
public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {

    private EditText et_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_edit_focus);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        this.setTitle("3.3 编辑框焦点操作");

        et_phone = this.findViewById(R.id.et_phone);
        EditText et_password = this.findViewById(R.id.et_password);

        //设置失去焦点事件
        et_password.setOnFocusChangeListener(this);

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            //获取焦点
            if (v.getId() == R.id.et_password) {
                //校验手机号
                String phone = et_phone.getText().toString();
                if (TextUtils.isEmpty(phone) || phone.length() != 11) {
                    // 手机号码编辑框请求焦点，也就是把光标移回手机号码编辑框
                    et_phone.requestFocus();
                    //提示信息
                    Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
                }
            }
        } else {
            //失去焦点

        }
    }
}